$ psql --host localhost --port 5432 --username laetitia \
--dbname test
psql (15devel)
Type "help" for help.
test=#
$ psql --host localhost
$ psql -h localhost
psql (15devel)
Type "help" for help.
laetitia=# \conninfo
You are connected to database "laetitia" as user "laetitia"
on host "localhost" (address "::1") at port "5432".
$ export PGHOST=localhost
$ psql
psql (15devel)
Type "help" for help.
laetitia=# \conninfo
You are connected to database "laetitia" as user "laetitia"
on host "localhost" (address "::1") at port "5432".
$ export PGHOST=
$ psql
psql (15devel)
Type "help" for help.
laetitia=# \conninfo
You are connected to database "laetitia" as user "laetitia"
via socket in "/tmp" at port "5432".
$ psql -h /tmp
psql (15devel)
Type "help" for help.
laetitia=# \conninfo
You are connected to database "laetitia" as user "laetitia"
via socket in "/tmp" at port "5432".
$ psql --port 5433
$ psql -p 5433
psql (15devel)
Type "help" for help.
laetitia=# \conninfo
You are connected to database "laetitia" as user "laetitia"
via socket in "/tmp" at port "5433".
$ export PGPORT=5433
$ psql
psql (15devel)
Type "help" for help.
laetitia=# \conninfo
You are connected to database "laetitia" as user "laetitia"
via socket in "/tmp" at port "5433".
$ export PGPORT=
$ psql
psql (15devel)
Type "help" for help.
laetitia=# \conninfo
You are connected to database "laetitia" as user "laetitia"
via socket in "/tmp" at port "5432".
$ psql --username test
$ psql -U test
psql (15devel)
Type "help" for help.
test=# \conninfo
You are connected to database "test" as user "test"
via socket in "/tmp" at port "5432".
$ export PGUSER=test
$ psql
psql (15devel)
Type "help" for help.
test=# \conninfo
You are connected to database "test" as user "test"
via socket in "/tmp" at port "5432".
$ export PGUSER=
$ psql
psql (15devel)
Type "help" for help.
laetitia=# \conninfo
You are connected to database "laetitia" as user "laetitia"
via socket in "/tmp" at port "5432".
$ psql test
$ psql --dbname test
$ psql -d test
psql (15devel)
Type "help" for help.
test=# \conninfo
You are connected to database "test" as user "laetitia"
via socket in "/tmp" at port "5432".
$ export PGDATABASE=test
$ psql
psql (15devel)
Type "help" for help.
test=# \conninfo
You are connected to database "test" as user "laetitia"
via socket in "/tmp" at port "5432".
$ export PGDATABASE=
$ psql
psql (15devel)
Type "help" for help.
laetitia=# \conninfo
You are connected to database "laetitia" as user "laetitia"
via socket in "/tmp" at port "5432".
$ psql postgresql://laetitia@localhost:5433/test
psql (15devel)
Type "help" for help.
test=# \conninfo
You are connected to database "test" as user "laetitia"
on host "localhost" (address "::1") at port "5433".
$ psql "user=laetitia host=localhost port=5433 dbname=test"
psql (15devel)
Type "help" for help.
test=# \conninfo
You are connected to database "test" as user "laetitia"
on host "localhost" (address "::1") at port "5433".
$ cat ~/.pg_service.conf
[mydb]
host=localhost
# Port is same as default but I could use port=5432
user=test
dbname=laetitia
psql service=mydb
psql (15devel)
Type "help" for help.
laetitia=> \conninfo
You are connected to database "laetitia" as user "test"
on host "localhost" (address "::1") at port "5432".
connection-type database user auth-method [auth-options]
connection-type database user address auth-method [auth-options]
For Debian-based (as root):
pg_lsclusters
For other systemD systems (as root):
systemctl status postgresql
For Windows:
Check the postgresql service
For others:
pg_ctl status -D [PGDATA]
For all except Windows:
pgrep -u postgres -fa -- -D
For Debian-based (as root):
pg_ctlcluster 14 [name] start
For other systemD systems (as root):
systemctl start postgresql
For Windows:
Start the postgresql service
For others (as postgres):
pg_ctl start -D [PGDATA]
For all except Windows (as root or postgres):
sed -n 4p [PGDATA]/postmaster.pid
or
For Debian-based (as root):
pg_lsclusters
listen_addresses
*
is not as insecure as you might
think(echo >/dev/tcp/[host]/[port]) &>/dev/null && \
echo "open" || echo "close"